package com.cloudkinto.controller.storage;


import com.cloudkinto.common.annotations.UserSessionRequired;
import com.cloudkinto.extentity.StorageRecordsAnalysisDto;
import com.cloudkinto.service.storage.vo.*;
import io.swagger.annotations.*;
import net.bytebuddy.asm.Advice;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.cloudkinto.service.storage.StorageShelfService;

import com.cloudkinto.common.web.PageResult;
import com.cloudkinto.common.web.SingleResult;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.annotation.Validated;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import com.cloudkinto.base.BaseController;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zhujiale
 * @since 2022-12-20
 */
@RestController
@Api(tags = "货架管理模块❤❤❤❤❤❤❤❤❤❤")
@RequestMapping("/api/admin/storage/shelf")
public class StorageShelfController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(StorageShelfController.class);

    @Autowired
    private StorageShelfService service;

    @UserSessionRequired
    @GetMapping("/addInit")
    @ApiOperation(value = "仓库初始化", notes = "仓库初始化")
    public SingleResult addInit() {
        return service.addInit();
    }

    @UserSessionRequired
    @PostMapping("/add")
    @ApiOperation(value = "新增货架", notes = "新增货架")
    public SingleResult add(@RequestBody @Validated StorageShelfAddReq req) {
        Long userId = getCurrentUserId();
        Long companyId = getCompanyId();
        return SingleResult.success("新增成功", service.add(req, userId, companyId));
    }

    @UserSessionRequired
    @GetMapping("/updateInit/{id}")
    @ApiOperation(value = "编辑初始化", notes = "编辑初始化")
    public SingleResult updateInit(@PathVariable("id") Long id) {
        return service.updateInit(id);
    }

    @UserSessionRequired
    @PutMapping("/update")
    @ApiOperation(value = "修改", notes = "修改")
    public SingleResult update(@RequestBody @Validated StorageShelfUpdateReq req) {
        Long userId = getCurrentUserId();
        Long companyId = getCompanyId();
        return SingleResult.success("修改成功", service.update(req, userId, companyId));
    }

    @UserSessionRequired
    @PutMapping("/updateSort")
    @ApiOperation(value = "设置拣货顺序", notes = "设置拣货顺序")
    public SingleResult updateSort(@RequestBody @Validated ShelfSortUpdateReq req) {
        Long userId = getCurrentUserId();
        service.updateSort(req, userId);
        return SingleResult.success("修改成功", "");
    }


    @UserSessionRequired
    @DeleteMapping("delete/{id}")
    @ApiOperation(value = "根据id删除", notes = "根据id删除")
    public SingleResult delete(@PathVariable("id") Long id) {
        Long userId = getCurrentUserId();
        return SingleResult.success("删除成功", service.delete(id, userId));
    }

    @UserSessionRequired
    @PutMapping("updateStatus")
    @ApiOperation(value = "启用禁用货架", notes = "启用禁用货架")
    public SingleResult updateStatus(@RequestBody @Validated UpdateShelfStatusReq req) {
        Long userId = getCurrentUserId();
        return SingleResult.success("修改成功", service.updateStatus(req, userId));
    }

    @UserSessionRequired
    @GetMapping("detail/{id}")
    @ApiOperation(value = "查看货架", notes = "查看货架")
    public SingleResult detail(@PathVariable("id") Long id) {
        return SingleResult.success("查询成功", service.detail(id));
    }


    @UserSessionRequired
    @GetMapping("/pageInit")
    @ApiOperation(value = "查询初始化", notes = "查询初始化")
    public SingleResult pageInit() {
        return service.pageInit();
    }

    @UserSessionRequired
    @GetMapping("/listPage")
    @ApiOperation(value = "查询（分页）", notes = "查询（分页）")
    @ApiImplicitParams({@ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "areaName", value = "区域名称（模糊查询）"),
            @ApiImplicitParam(name = "storageId", value = "仓库id"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小")})
    @ApiResponses({@ApiResponse(code = 200, message = "", response = StorageShelfRes.class)})
    public PageResult listPage(HttpServletRequest request) {
        Long companyId = getCompanyId();

        Map<String, String> map = new HashMap<>();
        String areaName = request.getParameter("areaName");
        String storageId = request.getParameter("storageId");
        String current = request.getParameter("current");
        String size = request.getParameter("pageSize");
        map.put("areaName", areaName);
        map.put("storageId", storageId);
        map.put("current", current);
        map.put("pageSize", size);
        map.put("companyId", null == companyId ? null : companyId + "");
        return service.getListPage(map);
    }

    @UserSessionRequired
    @GetMapping("/printBarCodePdf/{id}")
    @ApiOperation(value = "批量生成货架所有条形码", notes = "批量生成货架所有条形码")
    public void printBarCodePdf(@PathVariable("id") Long id, HttpServletResponse response) throws IOException {
        service.printBarCodePdf(id, response);
    }

    @UserSessionRequired
    @GetMapping("/printBarCodePdfOne/{id}")
    @ApiOperation(value = "生成单一库位条形码", notes = "生成单一库位条形码")
    public void printBarCodePdfOne(@PathVariable("id") Long id, HttpServletResponse response) throws IOException {
        service.printBarCodePdfOne(id, response);
    }


    @UserSessionRequired
    @GetMapping("getPickupPreview/{id}")
    @ApiOperation(value = "拣货预览", notes = "拣货预览")
    @ApiResponses(@ApiResponse(code = 200, message = "", response = PickupPreviewRes.class))
    public SingleResult getPickupPreview(@PathVariable("id") Long id) {
        return SingleResult.success("查询成功", service.getPickupPreview(id));
    }


    @UserSessionRequired
    @GetMapping("storageVolumePreview/{id}")
    @ApiOperation(value = "仓库预览", notes = "仓库预览")
    @ApiResponses(@ApiResponse(code = 200, message = "", response = StorageVolumePreviewRes.class))
    public SingleResult storageVolumePreview(@PathVariable("id") Long id) {
        return SingleResult.success("查询成功", service.storageVolumePreview(id));
    }


    @UserSessionRequired
    @PutMapping("setShowRuleColor")
    @ApiOperation(value = "修改库位一览展示规则", notes = "修改库位一览展示规则")
    public SingleResult setShowRuleColor(@RequestBody @Validated ShowRuleColorUpdateReq req) {
        Long userId = getCurrentUserId();
        service.setShowRuleColor(req, userId);
        return SingleResult.success("修改成功");
    }

}

